package manager
{
	import data.BulletProp;
	import data.Globals;
	import data.MonsterProp;
	import data.TankProp;
	import data.WaveProp;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;
	
	public class MapConfig
	{
		public var line:Array=[];
		
		private static var instance:MapConfig;
		
		private var xml:URLLoader;
		
		public var loaderComplete:Function;
		public function MapConfig()
		{
			if(instance) throw new Error("This is singelon!") ;
			instance = this;
		}
		
		public static function getInstance():MapConfig{
			return instance ||=new MapConfig();
		}
		
		public function loadXml(level:int):void
		{
			xml = new URLLoader();
			xml.load(new URLRequest("resources/"+level+".xml"+"?="+Math.random()*10000));
			xml.addEventListener(Event.COMPLETE,complete);
			xml.addEventListener(IOErrorEvent.IO_ERROR,io_error);
		}
		
		protected function io_error(event:IOErrorEvent):void
		{
			destroyEvent();
		}
		
		private function destroyEvent():void
		{
			xml.removeEventListener(Event.COMPLETE,complete);
			xml.removeEventListener(IOErrorEvent.IO_ERROR,io_error);
			xml = null;
		}
		
		public function complete(event:Event=null):void
		{
			destroyEvent();
			var xml:XML = XML(event.target.data);
			if(xml.hasOwnProperty("totalLevel")){
				Globals.totalLevel = xml.totalLevel;
			}
			
			if(xml.hasOwnProperty("initialEnergy")){
				Globals.initialEnergy = xml.initialEnergy;
			}
			var xmlList:XMLList;
			//解析路点
			var list:XMLList = xml.line.children();
			var xxx:XML;
			line.length = 0;
			for each(xxx in list){
				var point:Point = new Point(xxx.@x,xxx.@y);
				line.push(point);
			}
			
			//解析monster
			xmlList = xml.monster.children();
			for each(xxx in xmlList){
				var prop:MonsterProp = new MonsterProp();
				prop.name = xxx.@name;
				prop.blood = xxx.@blood;
				prop.speed = xxx.@speed;
				prop.energy = xxx.@energy;
				ObjDataManage.getInstance().monsterMap[prop.name] = prop;
			}
			//解析tank
			xmlList = xml.tank.children();
			for each(xxx in xmlList){
				var tp:TankProp = new TankProp();
				tp.name = xxx.@name;
				tp.bulletType = xxx.@bullet;
				tp.shoutGap = xxx.@shoutGap;
				tp.shoutRadius = xxx.@shoutRadius;
				tp.rotateSpeed = xxx.@rotateSpeed;
				tp.revive = xxx.@revive;
				ObjDataManage.getInstance().tankMap[tp.name] = tp;
			}
			//解析bullet
			xmlList = xml.bullet.children();
			for each(xxx in xmlList){
				var bp:BulletProp = new BulletProp();
				bp.name = xxx.@name;
				bp.art = xxx.@art;
				bp.speed = xxx.@speed;
				ObjDataManage.getInstance().bulletMap[bp.name] = bp;
			}
			//解析monster队列
			
			xmlList = xml.wave.children();
			var lens:int = xmlList.length();
			for(var i:int=0;i<lens;i++){
			    xxx = xmlList[i];
				var waveProp:WaveProp = new WaveProp();
				waveProp.gapTime = xxx.@gap;
				var ary1:Array = xxx.@list.split(",");
				while(ary1.length){
					var ary2:Array = ary1.shift().split("_");
					var monsterProp:MonsterProp = ObjDataManage.getInstance().getMonsterProp(ary2[0]);
					monsterProp.gapTime = int(ary2[1]);
					waveProp.list.push(monsterProp);
				}
				ObjDataManage.getInstance().monsterQueue.push(waveProp);
			}
			if(loaderComplete != null)loaderComplete();
		}
		
		public function copeline(ary:Array):void{
			ary.length = 0;
			for(var i:int=0;i<line.length;i++){
				ary.push(line[i]);
			}
		}
		
	}
}